const app = getApp();
var common = require("../../common.js")
var timer = null;
Page({

  /**
   * 页面的初始数据
   */
  jid: 0,
  data: {
    containerPx: app.globalData.containerPx,
    showTip: true,
    showResultTip: false,
    qa: [
      { q: '什么是对擂', a: '开启对擂时，要求好友必须充值1元钱作为保证金，如果好友成功找到目标，那么保证金退还好友，如果没有找到，则保证金在扣除平台服务费后归游戏发起人所有' },
      { q: '是否允许转发', a: '如禁止转发，那么游戏只能由您分享，您的好友不能再作分享，可以防止好友作弊唷' },
      { q: '奖金怎么提现', a: '点击提现，系统就会打款到您的微信钱包中' },
      { q: '没发出去的奖金会还给我吗', a: '游戏上线7天后，如奖金仍未发完，可以到游戏界面申请退回' },
      { q: '什么是平台服务费', a: '服务费由微信平台收取及支付服务器费用' },
    ],
    gid: 0,
    sel: { x: 0, y: 0 },
    img: { x: 0, y: 0 },
    scale: 1,
  },
  //出现
  showTip: function () {
    this.setData({ showTip: true })
  },
  //消失
  hideTip: function () {
    this.setData({ showTip: false })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (params) {
    console.log(typeof params)
    this.data.gid =  params.id;   
    this.startGroupGame();
  },


  startGroupGame: function () {
    var that = this;
    wx.showLoading({
      title: '正在获取游戏',
      mask: true,
    })
    common.request({
      url: 'c=Game&a=startSurenGame',
      data: { gid: that.data.gid },
      success: function (e) {
        if (e.status != 0) {
          wx.showModal({
            title: '错误',
            content: e.msg,
            complete: function () {
              wx.navigateTo({
                url: '../../pages/index/index',
              })
            }
          })
          return;
        }
        that.jid = e.jid;
        that.setData({
          target: e.target,
          relay: e.relay,
          avatarUrl: e.pic,
        });
        if (e.relay != 1) {//禁止转发
          wx.hideShareMenu({});
        }
        wx.getImageInfo({
          src: e.list,
          success: function (res) {
            console.log(res);
            var degree = 0;
            if (res.width > res.height) {
              degree = 90;
            }
            that.setData({
              notUploadImg: false,
              degree: degree,
              src: e.list,
              imgWidth: res.width,
              imgHeight: res.height
            })
          }
        })

        
      },
      complete: function (e) {
        wx.hideLoading();
      },
      method: "POST"
    });
  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '厉害了，找到"' + this.data.detail.target + '",还有机会赢取红包奖励',
      imageUrl: '../../image/f.jpg',
      path: 'pages/suren/begin?id=' + this.data.gid
    }
  },
  selectorMove: function (e) {
    this.getRect('#selector');
  },
  imgMove: function (e) {
    this.getRect('.img');
  },
  onScale: function (e) {
    this.data.scale = e.detail.scale;
    this.getRect('.img');
  },
  getRect: function (className) {
    console.log(className, '------------------');
    var that = this;
    wx.createSelectorQuery().select(className).boundingClientRect(function (rect) {
      console.log(rect);
      if (rect.id == 'selector') {
        that.data.sel.x = rect.left;
        that.data.sel.y = rect.top;
      } else {
        that.data.img.x = rect.left;
        that.data.img.y = rect.top;
      }
    }).exec()
  },
  onSubmit:function(){
    wx.showLoading({
      title: '正在提交',
      mask: true,
    })
    var that = this;
    var x = that.data.sel.x - that.data.img.x;
    var y = that.data.sel.y - that.data.img.y;
    var extra = x + ',' + y + ',' + that.data.scale;
    common.request({
      url: 'c=Game&a=submitSurenResult',
      data: {
        gid: that.data.gid,
        pic: extra,
        jid: that.jid,
      },
      success: function (e) {
        if (e.status != 0) {
          wx.showToast({
            title: e.msg,
            icon: 'none'
          })
          return;
        }
        if (e.result == 1) {
          var tip = '';
          if (e.prize == 0) {
            tip = '好可惜，红包奖励已经被抢完了';
          } else {
            tip = '获得' + e.prize + '元奖励,可到个人中心提现';
          }
          if (e.return != '0') {
            tip = ',您的保证金已原路退回';
          }
          that.setData({
            tipImg: '../../image/face_suc.jpg',
            tip1: '找对了，真厉害！',
            tip2: tip,
            showTip: false,
            showResultTip: true
          });
        } else {
          that.setData({
            tipImg: '../../image/face_fail.jpg',
            tip1: '真可惜，没有找对唷',
            tip2: '我与红包的距离只差0.01米',
            showTip: false,
            showResultTip: true
          });
        }

      },
      complete: function (ret) {
        wx.hideLoading();
      },
      method: "POST"
    });
  },
  createTap: function () {
    wx.reLaunch({
      url: '../index/index',
    })
  },
  homeTap: function () {
    wx.reLaunch({
      url: '../suren/home',
    })
  },
})